package org.lansir.bumppay;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.bumptech.bumpapi.BumpAPI;
import com.bumptech.bumpapi.BumpAPIListener;
import com.bumptech.bumpapi.BumpConnection;
import com.bumptech.bumpapi.BumpDisconnectReason;

public class BumpPayActivity extends Activity implements BumpAPIListener {

	private enum BumpType {
		PAY, RECEIVE
	}

	private Button btnPay;
	private Button btnReceive;
	private Button btnSettingAccount;

	public static final String API_KEY = "1a7cb850cfd44945b7704042e6682681";
	public static final int BUMP_API_REQUEST_PAY_CODE = 0;
	public static final int BUMP_API_REQUEST_RECEIVE_CODE = 1;
	
	public static final int DIALOG_SETTING_ACCOUNT = 1;
	private static final String ACCOUNT_KEY = "AliPayAccount";

	private BumpType bumpType;
	private Handler mHandler = new Handler();
	private BumpConnection conn;
	private SharedPreferences prefs;
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		bumpType = BumpType.PAY;
		btnPay = (Button) findViewById(R.id.btnPay);
		btnReceive = (Button) findViewById(R.id.btnReceive);
		btnSettingAccount = (Button) findViewById(R.id.btnAccount);
		prefs = getSharedPreferences("person",Context.MODE_PRIVATE);

		btnPay.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent bump = new Intent(BumpPayActivity.this, BumpAPI.class);
				bump.putExtra(BumpAPI.EXTRA_API_KEY, API_KEY);
				bump.putExtra(BumpAPI.EXTRA_USER_NAME, getResources()
						.getString(R.string.btn_pay));
				bump.putExtra(BumpAPI.EXTRA_ACTION_MSG,
						"Bump with another phone to share information.");
				startActivityForResult(bump, BUMP_API_REQUEST_PAY_CODE);
			}
		});
		btnReceive.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				try {
					Intent bump = new Intent(BumpPayActivity.this,
							BumpAPI.class);
					bump.putExtra(BumpAPI.EXTRA_API_KEY, API_KEY);
					bump.putExtra(BumpAPI.EXTRA_USER_NAME, getResources()
							.getString(R.string.btn_receive));
					bump.putExtra(BumpAPI.EXTRA_ACTION_MSG,
							"Bump with another phone to share information.");

					startActivityForResult(bump, BUMP_API_REQUEST_RECEIVE_CODE);
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		});
		
		btnSettingAccount.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				showDialog(DIALOG_SETTING_ACCOUNT);
			}
			
		});

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case BUMP_API_REQUEST_PAY_CODE:
				bumpType = BumpType.PAY;
				break;
			case BUMP_API_REQUEST_RECEIVE_CODE:
				bumpType = BumpType.RECEIVE;
				break;
			}
			conn = (BumpConnection) data
					.getParcelableExtra(BumpAPI.EXTRA_CONNECTION);
			conn.setListener(this, mHandler);
		} else if (data != null) {
//			BumpConnectFailedReason reason = (BumpConnectFailedReason) data
//					.getSerializableExtra(BumpAPI.EXTRA_REASON);
			Toast.makeText(this, getResources().getString(R.string.connection_failed), Toast.LENGTH_SHORT)
					.show();
		}

	}
	
	@Override
    protected Dialog onCreateDialog(int id) {
		switch(id){
		case DIALOG_SETTING_ACCOUNT:
			LayoutInflater factory = LayoutInflater.from(this);
            final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
            final EditText account = (EditText) textEntryView.findViewById(R.id.account_edit);
            account.setText(prefs.getString(ACCOUNT_KEY, ""));
            return new AlertDialog.Builder(this)
                .setIcon(R.drawable.icon)
                .setTitle(R.string.alert_dialog_setting_account)
                .setView(textEntryView)
                .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                    	Editor ed = prefs.edit();
                    	ed.putString(ACCOUNT_KEY, account.getText().toString());
                        /* User clicked OK so do some stuff */
                    }
                })
                .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        /* User clicked cancel so do some stuff */
                    }
                })
                .create();
		}
		return null;
	}

	public void bumpDataReceived(byte[] data) {
		switch(bumpType){
		case PAY:
			Toast.makeText(this, getResources().getString(R.string.transfer_success), Toast.LENGTH_SHORT).show();
			break;
		case RECEIVE:
			
			Toast.makeText(this, getResources().getString(R.string.transfer_success), Toast.LENGTH_SHORT).show();
			
			break;
		}
		
	}

	public void bumpDisconnect(BumpDisconnectReason reason) {
		Toast.makeText(this, getResources().getString(R.string.connection_failed), Toast.LENGTH_SHORT)
				.show();
	}
}